<?php
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);
include './vendor/autoload.php';
include './Download.php';
$client = new GuzzleHttp\Client();

$api = "https://user.999d.com/api/v2/xcxcx/hotlist?pgstart=0&pgoffset=5";

$data = json_decode(file_get_contents($api), true);

foreach ($data['data'] as $item) {
    $title = $item['title'];
    $path = './vedio/' . $item['title'];
    if (!file_exists($path)) {

        $vedioList = json_decode(file_get_contents('https://user.999d.com/api/v2/xcxcx/video/detail?id=' . $item['id'] . '&uid='), true);

        $vedioUrlRes = '';
        foreach ($vedioList['data']['download_url'] as $vedioUrl) {
            $url = Download::get_redirect_url($vedioUrl);
            if (strpos($url, '.mp4') !== false || strpos($url, '.MP4') !== false) {
                $vedioUrlRes = $url;
                break;
            }
        }
        if ($vedioUrlRes) {
            $res = mkdir($path, 0777, true);
            if ($res) {
                Download::testDowlowd($vedioUrlRes, $path . "./{$title}.mp4");
                Download:: getFile($vedioList['data']['thumb']['wide'], $path, $title.".jpg", 0);
            }
        }
        printf("end\n");

    }


}


